package com.gzx.plugin.tjzy.feign.project.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gzx.plugin.tjzy.feign.project.mapper.GzxProjectFeignMapper;
import com.gzx.plugin.tjzy.feign.project.service.GzxProjectFeignService;
import com.gzx.plugin.tjzy.modular.product.entity.GzxProduct;
import com.gzx.plugin.tjzy.modular.product.mapper.GzxProductMapper;
import com.gzx.tjzy.feign.resp.project.GzxProjectRespDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.tenant.TenantContext;

import java.util.List;

/**
 * @author zjt
 * @description
 * @date 2025/4/9 17:47
 */
@Service
@RequiredArgsConstructor
public class GzxProjectFeignServiceImpl implements GzxProjectFeignService {

    private final GzxProductMapper productMapper;

    private final GzxProjectFeignMapper projectFeignMapper;

    /**
     * 根据公司编码获取项目
     *
     * @param companyCode
     * @return
     */
    @Override
    @DS("tjzy")
    public List<GzxProjectRespDto> getProjectByCompany(String companyCode) {
        try {
            TenantContext.disableTenantFilter();
            List<GzxProduct> productList = productMapper.selectList(Wrappers.<GzxProduct>lambdaQuery()
                    .select(GzxProduct::getProductCode)
                    .eq(GzxProduct::getCompanyCode, companyCode));
            List<String> productCodeList = productList.stream().map(GzxProduct::getProductCode).toList();
            return projectFeignMapper.selectList(Wrappers.<GzxProjectRespDto>lambdaQuery()
                    .in(GzxProjectRespDto::getProductCode, productCodeList));
        } finally {
            TenantContext.enableTenantFilter();
            TenantContext.clear();
        }
    }

    /**
     * 获取所有项目的在催案件量和在催人力
     */
    @Override
    @DS("tjzy")
    public List<GzxProjectRespDto> listCaseDebt() {
        try {
            TenantContext.disableTenantFilter();
            return projectFeignMapper.listCaseDebt();
        } finally {
            TenantContext.enableTenantFilter();
            TenantContext.clear();
        }
    }

    /**
     * 获取所有项目
     */
    @Override
    @DS("tjzy")
    public List<GzxProjectRespDto> listCaseProject() {
        try {
            TenantContext.disableTenantFilter();
            return projectFeignMapper.listCaseProject();
        } finally {
            TenantContext.enableTenantFilter();
            TenantContext.clear();
        }
    }
}
